//
//  AMapPolygonsHandle.m
//  flutter_plugin_gdmap
//
//  Created by xiaoka on 2021/9/29.
//

#import "AMapPolygonsHandle.h"

@implementation AMapPolygonsHandle

+ (void)handleDrawPolygon:(NSDictionary *)arguments
                  mapView:(MAMapView *)mapView {
    // 地图控件内获取定位失败,获取flutter传过来的定位点
    NSData *locationData = [arguments[@"location"] dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *locationDic = [NSJSONSerialization JSONObjectWithData:locationData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:nil];
    // 暂时不处理
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([[locationDic objectForKey:@"latitude"] doubleValue],
                                                              [[locationDic objectForKey:@"longitude"] doubleValue]);
    
    // 围栏数据
    NSData *listData = [arguments[@"list"] dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *polygonDict = [NSJSONSerialization JSONObjectWithData:listData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:nil];
    
    NSArray *polygonList = [polygonDict objectForKey:@"polygonList"];
    NSMutableArray *polygonOverlays = [NSMutableArray array];
    for (NSArray *polygons in polygonList) {
        MAPolygon *polygonOverlay;
        NSMutableArray *hollowShapes = [NSMutableArray array];
        
        for (int i = 0; i < polygons.count; i++) {
            NSArray *points = polygons[i];
            // 经纬度数组
            CLLocationCoordinate2D *coorArr = malloc(sizeof(CLLocationCoordinate2D) * points.count);
            for (int i = 0; i < points.count; i++) {
                NSDictionary *pointDict = [points objectAtIndex:i];
                CGFloat longitude = [[pointDict objectForKey:@"longitude"] floatValue];
                CGFloat latitude  = [[pointDict objectForKey:@"latitude"] floatValue];
                coorArr[i] = CLLocationCoordinate2DMake(latitude, longitude);
            }
            
            // 主围栏
            if (i == 0) {
                polygonOverlay = [MAPolygon polygonWithCoordinates:coorArr count:points.count];
            }
            // 缕空围栏
            else {
                MAPolygon *hollowPoly = [MAPolygon polygonWithCoordinates:coorArr count:points.count];
                [hollowShapes addObject:hollowPoly];
            }
            free(coorArr);
            coorArr = nil;
        }
        if (hollowShapes.count > 0) polygonOverlay.hollowShapes = hollowShapes;
        
        [polygonOverlays addObject:polygonOverlay];
    }
    // 添加覆盖物
    [mapView addOverlays:polygonOverlays level:MAOverlayLevelAboveRoads];
    // 收缩地图范围
    NSMutableArray *overlays = [NSMutableArray arrayWithArray:polygonOverlays];
    [overlays addObject:mapView.userLocationAccuracyCircle];
    
    [mapView setVisibleMapRect:[CommonUtility mapRectForOverlays:overlays]
                    edgePadding:UIEdgeInsetsMake(20, 20, 20, 20)
                       animated:YES];
}

+ (void)handlePolygonInside:(NSDictionary *)arguments
                    mapView:(MAMapView *)mapView {
    // 围栏数据
    NSData *listData = [arguments[@"list"] dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *polygonDict = [NSJSONSerialization JSONObjectWithData:listData
                                                                options:NSJSONReadingMutableContainers
                                                                  error:nil];
    
    NSArray *polygonList = [polygonDict objectForKey:@"polygonList"];
    NSMutableArray *polygonOverlays = [NSMutableArray array];
    for (NSArray *polygons in polygonList) {
        MAPolygon *polygonOverlay;
        
        for (int i = 0; i < polygons.count; i++) {
            NSArray *points = polygons[i];
            // 经纬度数组
            CLLocationCoordinate2D *coorArr = malloc(sizeof(CLLocationCoordinate2D) * points.count);
            for (int i = 0; i < points.count; i++) {
                NSDictionary *pointDict = [points objectAtIndex:i];
                CGFloat longitude = [[pointDict objectForKey:@"longitude"] floatValue];
                CGFloat latitude  = [[pointDict objectForKey:@"latitude"] floatValue];
                coorArr[i] = CLLocationCoordinate2DMake(latitude, longitude);
            }
            
            // 主围栏
            if (i == 0) {
                polygonOverlay = [MAPolygon polygonWithCoordinates:coorArr count:points.count];
            }
            
            free(coorArr);
            coorArr = nil;
        }
        [polygonOverlays addObject:polygonOverlay];
    }
    
    [mapView setVisibleMapRect:[CommonUtility mapRectForOverlays:polygonOverlays]
                    edgePadding:UIEdgeInsetsMake(20, 20, 20, 20)
                       animated:YES];
}

@end
